<template>
  <div class="hotel-detail-container">
    <!-- 酒店基本信息 -->
    <div v-if="hotelInfo.id" class="hotel-info-section">
      <div class="hotel-header">
        <h1>{{ hotelInfo.name }}</h1>
        <div class="star-rating">
          <el-rate v-model="hotelInfo.starRating" disabled text-color="#ff9900" />
        </div>
      </div>
      
      <div class="hotel-info-card">
        <div class="hotel-image">
          <img :src="getHotelImage(hotelInfo)" alt="酒店图片">
        </div>
        
        <div class="hotel-info">
          <p><i class="el-icon-location"></i> <b>地址：</b>{{ hotelInfo.address }}</p>
          <p v-if="hotelInfo.description"><b>简介：</b>{{ hotelInfo.description }}</p>
          <p><b>创建时间：</b>{{ formatDate(hotelInfo.create_time) }}</p>
        </div>
      </div>
    </div>

    <!-- 房间列表 -->
    <div class="room-list-section">
      <h2>可预订房间</h2>
      <el-row :gutter="20">
        <el-col :span="8" v-for="room in roomList" :key="room.id">
          <el-card class="room-card" shadow="hover">
            <div class="room-image">
              <img :src="getRoomImage(room)" alt="房间图片">
            </div>
            <div class="room-info">
              <h3>{{ room.room_type }}</h3>
              <p class="room-price">¥{{ room.price }}/晚</p>
              <p class="room-amenities" v-if="room.amenities">
                <span v-if="getAmenities(room).WiFi"><el-tag size="small">WiFi: {{ getAmenities(room).WiFi }}</el-tag></span>
                <span v-if="getAmenities(room).床型"><el-tag size="small">{{ getAmenities(room).床型 }}</el-tag></span>
                <span v-if="getAmenities(room).早餐"><el-tag size="small">早餐: {{ getAmenities(room).早餐 }}</el-tag></span>
                <span v-if="getAmenities(room).面积"><el-tag size="small">{{ getAmenities(room).面积 }}</el-tag></span>
              </p>
              <p>剩余: {{ room.quantity }}间</p>
              <div class="room-actions">
                <el-button type="primary" @click="bookRoom(room)" :disabled="room.quantity <= 0">
                  预订
                </el-button>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      
      <!-- 无房间提示 -->
      <el-empty v-if="roomList.length === 0" description="暂无可预订的房间"></el-empty>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import request from '@/utils/request.js';
import { getHotelImageUrl, getRoomImageUrl } from '@/utils/imageUtil.js';
import { ElMessage } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';

const route = useRoute();
const router = useRouter();
const hotelInfo = ref({});
const roomList = ref([]);

// 获取酒店图片
const getHotelImage = (hotel) => {
  return getHotelImageUrl(hotel);
};

// 获取房间图片
const getRoomImage = (room) => {
  return getRoomImageUrl(room);
};

// 解析房间设施JSON
const getAmenities = (room) => {
  if (room.amenities && typeof room.amenities === 'string') {
    try {
      return JSON.parse(room.amenities);
    } catch (e) {
      return {};
    }
  } else if (room.amenities && typeof room.amenities === 'object') {
    return room.amenities;
  }
  return {};
};

// 日期格式化
const formatDate = (dateStr) => {
  if (!dateStr) return '';
  const date = new Date(dateStr);
  return date.toLocaleDateString();
};

// 加载酒店详情
const loadHotelInfo = async () => {
  try {
    const hotelId = route.params.id;
    const res = await request.get(`/hotel/selectById/${hotelId}`);
    hotelInfo.value = res.data;
  } catch (error) {
    ElMessage.error('获取酒店详情失败，请稍后重试');
  }
};

// 加载房间列表
const loadRooms = async () => {
  try {
    const hotelId = route.params.id;
    const res = await request.get('/room/selectPage', {
      params: {
        hotelId: hotelId,
        pageNum: 1,
        pageSize: 100
      }
    });
    roomList.value = res.data.list || [];
  } catch (error) {
    ElMessage.error('获取房间列表失败，请稍后重试');
  }
};

// 预订房间
const bookRoom = (room) => {
  router.push({
    name: 'BookRoom',
    query: {
      hotelId: hotelInfo.value.id,
      roomId: room.id,
      price: room.price,
      roomType: room.room_type
    }
  });
};

onMounted(async () => {
  await loadHotelInfo();
  await loadRooms();
});
</script>

<style scoped>
.hotel-detail-container {
  padding: 20px;
}

.hotel-info-section {
  margin-bottom: 30px;
}

.hotel-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.hotel-header h1 {
  margin: 0;
  margin-right: 15px;
}

.hotel-info-card {
  display: flex;
  background: #f9f9f9;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.hotel-image {
  width: 350px;
  height: 250px;
  overflow: hidden;
}

.hotel-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.hotel-info {
  flex: 1;
  padding: 20px;
}

.hotel-info p {
  margin: 10px 0;
  line-height: 1.6;
}

.room-list-section {
  margin-top: 40px;
}

.room-list-section h2 {
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.room-card {
  height: 100%;
  margin-bottom: 20px;
}

.room-image {
  height: 180px;
  overflow: hidden;
}

.room-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.room-image img:hover {
  transform: scale(1.05);
}

.room-info {
  padding: 15px 0;
}

.room-info h3 {
  margin-top: 0;
  margin-bottom: 10px;
}

.room-price {
  color: #ff6700;
  font-weight: bold;
  font-size: 18px;
  margin-bottom: 10px;
}

.room-amenities {
  margin-bottom: 10px;
}

.room-amenities span {
  margin-right: 5px;
  margin-bottom: 5px;
  display: inline-block;
}

.room-actions {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>